#include "plistparser.h"


PlistParser::PlistParser(QObject *parent):
    QObject(parent)
{
}

PlistParser::~PlistParser()
{
    PlistFrameInfoList::iterator it;
    for(it = m_sourceFrameList.begin(); it != m_sourceFrameList.end(); it++)
    {
        delete *it;
    }
    m_sourceFrameList.clear();

}

void PlistParser::parsePointInfo(QDomElement &pointElement, QPointF& point)
{
    QString offsetInfo = pointElement.text();
    offsetInfo.remove("{");
    offsetInfo.remove("}");
    QStringList list = offsetInfo.split(",");

    point.setX(list.at(XSLOT).toInt());
    point.setY(list.at(YSLOT).toInt());
}

void PlistParser::parseSizeInfo(QDomElement &sizeElement, QRectF& rect)
{
    QString sizeInfo = sizeElement.text();
    sizeInfo.remove("{");
    sizeInfo.remove("}");
    QStringList sizeInfoList = sizeInfo.split(",");

    rect.setX(sizeInfoList.at(XSLOT).toInt());
    rect.setY(sizeInfoList.at(YSLOT).toInt());
    rect.setWidth(sizeInfoList.at(WSLOT).toInt());
    rect.setHeight(sizeInfoList.at(HSLOT).toInt());
}

void PlistParser::parseTexturePackPlist(QDomNode& dictNode,QString fileName)
{
    QDomElement dicElement = dictNode.toElement();
    QString pngName = dicElement.childNodes().at(3).childNodes().at(3).toElement().text();

    QDomNodeList framesElemetList = dicElement.childNodes().at(1).childNodes();

    for(int i = 0; i < framesElemetList.length(); i+=2)
    {
       PlistFrameInfo* frameInfo = new PlistFrameInfo();
       frameInfo->name = framesElemetList.at(i).toElement().text();
       frameInfo->plistName = fileName.split("/").last();
       frameInfo->plistPngName = pngName;

       QDomElement sizeElement = framesElemetList.at(i + 1).childNodes().at(1).toElement();
       parseSizeInfo(sizeElement, frameInfo->sizeRect);

       QDomElement offsetElement =  framesElemetList.at(i + 1).childNodes().at(3).toElement();
       parsePointInfo(offsetElement, frameInfo->offset);

       QDomElement rotateElement = framesElemetList.at(i + 1).childNodes().at(5).toElement();
       bool isRotated = (rotateElement.tagName() == "true");

       QDomElement sourceColorRectElement = framesElemetList.at(i + 1).childNodes().at(7).toElement();
       parseSizeInfo(sourceColorRectElement, frameInfo->sourceColorRect);

       QDomElement sourceRectElement = framesElemetList.at(i + 1).childNodes().at(9).toElement();
       parsePointInfo(sourceRectElement, frameInfo->sourceRectPoint);

       frameInfo->isRotated = isRotated;

       m_sourceFrameList.append(frameInfo);
    }
}

void PlistParser::parseZwoptexPList(QDomNode &dictNode,QString fileName)
{
    QDomElement dicElement = dictNode.toElement();
    QString pngName = dicElement.childNodes().at(0).childNodes().at(3).toElement().text();

    QDomNodeList framesElemetList = dicElement.childNodes().at(1).childNodes();

    for(int i = 0; i < framesElemetList.length(); i+=2)
    {
       PlistFrameInfo* frameInfo = new PlistFrameInfo();
       frameInfo->name = framesElemetList.at(i).toElement().text();
       frameInfo->plistName = fileName.split("/").last();
       frameInfo->plistPngName = pngName;

       QDomElement sizeElement = framesElemetList.at(i + 1).childNodes().at(1).toElement();
       parseSizeInfo(sizeElement, frameInfo->sizeRect);

       QDomElement offsetElement =  framesElemetList.at(i + 1).childNodes().at(3).toElement();
       parsePointInfo(offsetElement, frameInfo->offset);

       QDomElement rotateElement = framesElemetList.at(i + 1).childNodes().at(5).toElement();
       bool isRotated = (rotateElement.tagName() == "true");

       QDomElement sourceColorRectElement = framesElemetList.at(i + 1).childNodes().at(7).toElement();
       parseSizeInfo(sourceColorRectElement, frameInfo->sourceColorRect);

       QDomElement sourceRectElement = framesElemetList.at(i + 1).childNodes().at(9).toElement();
       parsePointInfo(sourceRectElement, frameInfo->sourceRectPoint);

       frameInfo->isRotated = isRotated;

       m_sourceFrameList.append(frameInfo);
    }
}

bool PlistParser::parsePlistFile(const QString& fileName)
{
    QDomDocument document;

    QFile file(fileName);    
    if(!file.open(QIODevice::ReadOnly))
    {
        return false;
    }

    if(!document.setContent(&file))
    {
        file.close();
        return false;
    }
    file.close();

    QDomNode plistElement = document.childNodes().at(1);

    QDomNode dictNode = plistElement.childNodes().at(0);

    QString firstKey = dictNode.childNodes().at(0).toElement().text();

//    if(firstKey == "texture")
//    {
//        parseZwoptexPList(dictNode,fileName);
//    }
//    else  if(firstKey == "frames")
    {
        parseTexturePackPlist(dictNode,fileName);
    }

    emit notifyPlistDataChanged();
    return true;
}



